package com.mogist.hqc.module.personal.ui

import android.app.Dialog
import android.content.Intent
import android.support.v7.widget.LinearLayoutManager
import android.view.Gravity
import android.view.WindowManager
import android.widget.TextView
import com.chad.library.adapter.base.BaseQuickAdapter
import com.mogist.hqc.R
import com.mogist.hqc.base.BaseMVPActivity
import com.mogist.hqc.module.personal.adapter.MyCommentAdapter
import com.mogist.hqc.module.personal.contract.MyCommentListContract
import com.mogist.hqc.module.personal.presenter.MyCommentListPresenter
import com.mogist.hqc.test.TestData
import com.mogist.hqc.test.TestDto
import kotlinx.android.synthetic.main.activity_comment_list.*
import kotlinx.android.synthetic.main.activity_my_list.*
import kotlinx.android.synthetic.main.activity_my_list.mSwipeToLoadLayout
import kotlinx.android.synthetic.main.activity_my_list.swipe_target
import kotlinx.android.synthetic.main.include_title.*
import talex.zsw.basecore.view.other.RxToast
import talex.zsw.basecore.view.other.swipetoloadlayout.OnLoadMoreListener
import talex.zsw.basecore.view.other.swipetoloadlayout.OnRefreshListener
import talex.zsw.basecore.view.recyleview.DividerItemDecoration
import talex.zsw.basecore.view.recyleview.SampleFooter
import talex.zsw.basecore.view.recyleview.SampleHeader

/**
 * 作用： 评论列表
 * 作者：赵小白 email:vvtale@gmail.com  
 * 修改人：
 * 修改时间：
 * 修改备注：
 */
class MyCommentListActivity : BaseMVPActivity<MyCommentListContract.Presenter>(), MyCommentListContract.View, OnRefreshListener, OnLoadMoreListener
{
    override fun initArgs(intent: Intent)
    {
    }

    override fun initView()
    {
        setContentView(R.layout.activity_my_list)
        mPresenter = MyCommentListPresenter(this)
    }

    override fun initData()
    {
        mTitleBar.setTitle("我的评论")
        initRecyclerView()
        adapter.replaceData(TestData.getScenics(10))
    }

    private val adapter = MyCommentAdapter()

    private fun initRecyclerView()
    {
        adapter.setOnItemChildClickListener { adapter, view, position ->
            if (!isFastClick)
            {
                when (view.id)
                {
                    R.id.iIvMore   ->
                    {
                        showDeleteDialog()
                    }
                    R.id.iLLDetail ->
                    { // 详情
                        RxToast.normal("详情")
                    }
                }
            }
        }
        adapter.emptyView =
                SampleHeader(this@MyCommentListActivity, swipe_target.width, swipe_target.height)
        adapter.isUseEmpty(false)
        adapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN)
        swipe_target.addItemDecoration(DividerItemDecoration(DividerItemDecoration.VERTICAL_LIST,
                                                             R.color.transparent))
        swipe_target.layoutManager = LinearLayoutManager(this@MyCommentListActivity)
        swipe_target.adapter = adapter
        // mRecyclerView.isNestedScrollingEnabled = false
        mSwipeToLoadLayout.setOnRefreshListener(this)
        mSwipeToLoadLayout.setOnLoadMoreListener(this)
        mSwipeToLoadLayout.isLoadMoreEnabled = true
        mSwipeToLoadLayout.isRefreshEnabled = true
    }

    private var dialog: Dialog? = null
    private fun showDeleteDialog()
    {
        dialog
                ?: let {
                    dialog = Dialog(this@MyCommentListActivity, R.style.dialog)
                    dialog?.setContentView(R.layout.dialog_delete)
                    dialog?.setCanceledOnTouchOutside(true)
                    val display = window.windowManager.defaultDisplay
                    val layoutParams: WindowManager.LayoutParams = dialog?.window!!.attributes
                    layoutParams.width = (display.width * 1f).toInt()
                    // layoutParams.height = (display.height * 0.8f).toInt()
                    layoutParams.gravity = Gravity.BOTTOM
                    if (layoutParams.gravity == Gravity.BOTTOM)
                    {
                        layoutParams.y = 0
                    }
                    dialog?.window!!.attributes = layoutParams
                    dialog?.findViewById<TextView>(R.id.dTvDelete)
                            ?.setOnClickListener { }
                    dialog?.findViewById<TextView>(R.id.dTvCancel)
                            ?.setOnClickListener {
                                dialog?.dismiss()
                            }
                }
        dialog?.show()
    }

    // --------------- 数据获取 ---------------
    override fun onDestroy()
    {
        super.onDestroy()
        adapter.removeAllFooterView()
        sampleFooter = null
    }

    override fun getData(silence: Boolean)
    {

    }

    var sampleFooter: SampleFooter? = null
    override fun bindData(list: ArrayList<TestDto>?)
    {
        adapter.isUseEmpty(true)
        if (list != null && list.isNotEmpty())
        {
            if (page == 1)
            {
                adapter.setNewData(list)
            }
            else
            {
                adapter.addData(list)
            }
            //            if ()
            //            {
            //                sampleFooter?:let {
            //                    sampleFooter = SampleFooter(this@MyCommentListActivity)
            //                }
            //                adapter.setFooterView(sampleFooter)
            //                mSwipeToLoadLayout.isLoadMoreEnabled = false
            //            }
            page++
        }
        else
        {
            mSwipeToLoadLayout.isLoadMoreEnabled = false
        }
    }

    private var page = 1

    override fun disLoading()
    {
        mSwipeToLoadLayout.isLoadingMore = false
        mSwipeToLoadLayout.isRefreshing = false
    }

    override fun onRefresh()
    {
        adapter.removeAllFooterView()
        page = 1
        mSwipeToLoadLayout.isLoadMoreEnabled = true
        mSwipeToLoadLayout.isRefreshEnabled = true
        getData(true)
    }

    override fun onLoadMore()
    {
        getData(true)
    }

}